package com.bw.module_home.ui.marketers

import android.content.DialogInterface
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.widget.PopupMenu
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.module_home.R
import com.bw.module_home.databinding.ActivityMarketersBinding
import com.bw.module_home.marketer.adapter.BusinessProfileAdapter
import com.bw.module_home.marketer.viewmodel.MarketersViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.Ui.MarketersActivity)
class MarketersActivity : BaseActivity<MarketersViewModel, ActivityMarketersBinding>() {
    lateinit var businessProfileAdapter: BusinessProfileAdapter
    private val viewModel: MarketersViewModel by viewModels()

    override val mLayoutResId: Int
        get() = R.layout.activity_marketers

    override fun initView() {
        super.initView()
        mBinding.back.setOnClickListener {
            finish()
        }
        businessProfileAdapter = BusinessProfileAdapter()
        mBinding.rvBusinessProfile.layoutManager = LinearLayoutManager(this)
        mBinding.rvBusinessProfile.adapter = businessProfileAdapter
        viewModel.getBusinessProfile()

        businessProfileAdapter.setOnItemLongClickListener { adapter, view, position ->
            AlertDialog.Builder(this)
                .setTitle("是否删除商家资料?")
                .setMessage("删除后将无法恢复，请谨慎操作！")
                .setPositiveButton("确定") { dialog: DialogInterface?, which: Int ->
                    viewModel.deleteBusinessProfile(businessProfileAdapter.data[position].id)
                }
                .setNegativeButton("取消", null)
                .show()
            true
        }
        businessProfileAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build(Router.Ui.MarketersDetailActivity)
                .withInt("id", businessProfileAdapter.data[position].id)
                .navigation()
        }
//        mBinding.addBusiness.setOnClickListener {
//            ARouter.getInstance().build("/module_marketers/residencyApplicationEditActivity").navigation()
//        }
        mBinding.add.setOnClickListener {v->
            val popupMenu = PopupMenu(this,v)
            popupMenu.menuInflater.inflate(R.menu.marketers_menu,popupMenu.menu)
            popupMenu.setOnMenuItemClickListener { item ->
                when (item.itemId) {
                    R.id.add_business ->{
                        ARouter.getInstance().build("/module_marketers/residencyApplicationEditActivity").navigation()
                    }
                    R.id.add_profile ->{
                        ARouter.getInstance().build("/module_marketers/businessProfileAddActivity").navigation()
                    }
                }
                true
            }
            popupMenu.show()
        }

        mBinding.etSearch.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                if (p0.toString().isNotEmpty()) {
                    val list = businessProfileAdapter.data.filter {
                        it.name.contains(p0.toString())
                    }
                    businessProfileAdapter.data.clear()
                    businessProfileAdapter.data.addAll(list)
                    businessProfileAdapter.notifyDataSetChanged()
                } else {
                    viewModel.getBusinessProfile()
                }
            }

            override fun afterTextChanged(p0: Editable?) {

            }
        })
    }

    override fun initListener() {
        super.initListener()
        viewModel.getBusinessProfileSuccess.observe(this) {
            businessProfileAdapter.data.clear()
            businessProfileAdapter.data.addAll(it)
            businessProfileAdapter.notifyDataSetChanged()
        }
    }
}